<?php

namespace IDock\Insurance\DBIC\Request;

use IDock\Insurance\DBIC\Response\PremiumInquirySaveResponse;
use IDock\Visitor\Premium\PremiumInquiryVisitor;
use IDock\Visitor\Premium\PremiumRelationVisitor;
use IDock\Visitor\SpecialClausesVisitor;

class PremiumInquirySaveRequest extends AllDBICRequest
{
    protected $route = 'dbic/qos/channel/quote/preSave';

    protected $response = PremiumInquirySaveResponse::class;

    protected $urlTip = '询价单保存';

    /*** @var  PremiumInquiryVisitor $visitor */
    protected $visitor;

    public function generate(): array
    {
        return [
            'main' => [
                'calculateNo' => $this->visitor->getExtInfoVisitor()->argc['calculateNo'] ?? '',
                'belongComCode' => $this->visitor->getExtInfoVisitor()->argc['belongComCode'] ?? '',
                'belongUserCode' => $this->visitor->getExtInfoVisitor()->argc['belongUserCode'] ?? '',
            ],
            'car' => [
                'jym' => '',
                'brandAlias' => '',
                'useNatureCodeSub' => '',
            ],
            'engages' => $this->specialClausesList(),

            'insures' => [
                $this->relation($this->visitor->getApplicantVisitor(), '2'),
                $this->relation($this->visitor->getInsuredVisitor(), '1'),
                $this->relation($this->visitor->getOwnerVisitor(), '3'),
            ]
        ];
    }

    /**
     * 关系人信息
     * @param PremiumRelationVisitor $visitor
     * @param string $insureType
     * @return array
     */
    private function relation(PremiumRelationVisitor $visitor, string $insureType): array
    {
        return [
            'serialNo' => $insureType,
            'insureFlag' => $insureType,
            'mobileNumber' => $visitor->mobile,
            'emailAddress' => $visitor->email,
            'address' => $visitor->address,
            'idValidDate' => $visitor->certificateEndDate,
            'effectiveDate' => $visitor->certificateStartDate,
            'postCode' => $visitor->postcode,
            'occupationCode' => '1212',//职业
            'occupation' => '个体经营者（零售批发业）',//职业
            'nationality' => '个体经营者（零售批发业）',//职业
        ];
    }

    /**
     * 特约列表
     * @return array
     */
    private function specialClausesList(): array
    {
        $list = [];
        $no = 1;
        foreach ($this->visitor->getUnderwritingVisitor()->specialClausesList as $specialClauses) {
            /*** @var SpecialClausesVisitor $specialClauses */
            $list[] = [
                'serialNo' => (string)$no,
                'riskType' => $specialClauses->riskType == 'VCI' ? '1' : '2',
                'clauseCode' => $specialClauses->clauseCode,
                'title' => $specialClauses->clauseName,
                'content' => $specialClauses->clauseContent,
            ];
            $no += 1;
        }

        return $list;
    }
}
